/**
 *
 */
package idao.support.spring;

import idao.IDAO;
import idao.IDAOFactory;

import javax.sql.DataSource;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * A {@link FactoryBean} implementation for using with spring framework.
 * 
 * @author huhao
 * @see FactoryBean
 */
public class DaoFactoryBean<I> extends AbstractFactoryBean
{

    private Class<I> dao;
    private IDAO iDao;

    public DaoFactoryBean(Class<I> dao, DataSource ds)
    {
        if ((dao == null) || (ds == null)) {
            throw new NullPointerException("Both dao && datasource must not be null!");
        }
        this.dao = dao;
        this.iDao = IDAOFactory.newIDAO(new SpringAwareConnectionManager(ds));
    }

    @Override
    protected I createInstance() throws Exception
    {
        return iDao.newDao(dao);
    }

    @Override
    public Class<I> getObjectType()
    {
        return dao;
    }
}
